﻿/****************************** Module Header ******************************\
 * Module Name:  TaskBarNotifier.cs
 * Module Version:  1.0.0 (2012-10-5)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the TaskBarNotifier.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace UCF.Core.Forms.CustomControls.Notifier
{
    public partial class SoTaskBarNotifier : Form
    {
        private int closeEffect;
        public SoTaskBarNotifier()
        {
            InitializeComponent();
        }

        private void TaskBarNotifier_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 运行通知栏
        /// </summary>
        /// <param name="speedInterval">速度间隔，值越小越快</param>
        /// <param name="stopTime">通知栏停止时间</param>
        /// <param name="hideInterval">通知栏关闭速度间隔，值越小越快</param>
        /// <param name="closeEffect">通知栏关闭效果,1=渐隐式,2=下沉式</param>
        public void Run(int speedInterval = 5, int stopTime = 3000, int hideInterval = 200, int closeEffect = 1)
        {
            this.closeEffect = closeEffect;
            tiClose.Interval = hideInterval;
            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height);
            this.PointToScreen(p);
            this.Location = p;
            this.Show();

            for (int i = 0; i <= this.Height; i++)
            {
                this.Location = new Point(p.X, p.Y - i);
                Thread.Sleep(speedInterval);
            }
            Thread.Sleep(stopTime);
        }

        // 定时关闭窗体
        private void tiClose_Tick(object sender, EventArgs e)
        {
            tiStop.Enabled = false;
            if (MousePosition.X >= this.Location.X && MousePosition.Y >= this.Location.Y)
            {
                tiStop.Enabled = true;
                tiClose.Enabled = false;
            }
            // 窗体关闭样式
            if (this.closeEffect == 1)
            {
                this.CloseStyleOpacity();
            }
            else
            {
                this.CloseStyleDown();
            }
        }

        // 鼠标悬浮停止关闭窗体
        private void tiStop_Tick(object sender, EventArgs e)
        {
            tiClose.Enabled = false;
            if(this.closeEffect==1) this.Opacity = 1;
            if (this.closeEffect == 2) this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height-this.Height);
            if (MousePosition.X < this.Location.X || MousePosition.Y < this.Location.Y)
            {
                tiClose.Enabled = true;
                tiStop.Enabled = false;
            }
        }

        // 窗体关闭样式-渐隐式
        private void CloseStyleOpacity()
        {
            if (this.Opacity > 0 && this.Opacity <= 1)
            {
                this.Opacity = this.Opacity - 0.1;
            }
            if (this.Opacity == 0)
            {
                this.Close();
            }
        }

        // 窗体关闭样式-下沉式
        private void CloseStyleDown()
        {
            Point p;
            for (int i = 0; i <= this.Height; i++)
            {
                if (MousePosition.X >= this.Location.X && MousePosition.Y >= this.Location.Y) return;
                p = new Point(this.Location.X, this.Location.Y + 2);
                this.PointToScreen(p);
                this.Location = p;
                Thread.Sleep(10);
            }
            this.Close();
        }
    }
}
